package com.dounine.clouddisk360.parser;

import java.io.IOException;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dounine.clouddisk360.parser.deserializer.passport.Passport;
import com.dounine.clouddisk360.parser.deserializer.passport.PassportConfig;
import com.dounine.clouddisk360.parser.deserializer.passport.PassportRequestInterceptor;
import com.dounine.clouddisk360.parser.deserializer.passport.PassportResponseHandle;
import com.dounine.clouddisk360.pool.PoolingHttpClientConnection;

public class PassportParser extends BaseParser<PassportConfig> {

	private static final Logger LOGGER = LoggerFactory.getLogger(PassportParser.class);

	@Override
	public void initConfig(PassportConfig passportConfig) {
		super.initConfig(passportConfig);// 必需调用父类的initConfig方法,或者重写里面所定义的cookieStore路径
	}

	public Passport parse(String uri) {
		return execute(initRequest(uri));
	}
	
	public HttpGet initRequest(String uri){
		return new HttpGet(uri);
	}

	public Passport execute(HttpGet request) {
		super.httpClient = HttpClients.custom()
				.setConnectionManager(PoolingHttpClientConnection.getInstalce())
				.addInterceptorLast(new PassportRequestInterceptor())
				.setDefaultRequestConfig(cookieRequestConfig())
				.setDefaultCookieStore(super.httpClientContext.getCookieStore()).build();
		try {
			return super.httpClient.execute(request,new PassportResponseHandle(this), super.httpClientContext);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
